package com.vito.framework.system.config.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.vito.framework.base.service.impl.BaseServiceImpl;
import com.vito.framework.cache.CacheKeys;
import com.vito.framework.cache.ICache;
import com.vito.framework.system.config.mapper.ConfMapper;
import com.vito.framework.system.config.model.SysConfig;
import com.vito.framework.system.config.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysConfigServiceImpl extends BaseServiceImpl<ConfMapper, SysConfig> implements SysConfigService {

    @Autowired
    protected ICache cacheUtil;

    @Resource
    ConfMapper confMapper;

    @Override
    public SysConfig get() {
        SysConfig config = cacheUtil.get(CacheKeys.SYS_CONFIG);
        if (config == null) {
            List<SysConfig> list = list();
            if (list != null && list.size() > 0) {
                config = list.get(0);
                cacheUtil.put(CacheKeys.SYS_CONFIG, config, 60 * 60 * 24 * 10);
            }
        }
        return config;
    }

    @Override
    public int insert(SysConfig sysConfig) {
        cacheUtil.put(CacheKeys.SYS_CONFIG, sysConfig, 60 * 60 * 24 * 10);
        return confMapper.insert(sysConfig);
    }

    @Override
    public int update(SysConfig sysConfig) {
        cacheUtil.put(CacheKeys.SYS_CONFIG, sysConfig, 60 * 60 * 24 * 10);
        return confMapper.update(sysConfig, new LambdaUpdateWrapper<SysConfig>().eq(SysConfig::getId, sysConfig.getId()));
    }

    @Override
    public int delete(SysConfig sysConfig) {
        cacheUtil.delete(CacheKeys.SYS_CONFIG);
        return confMapper.delete(new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getId, sysConfig.getId()));
    }
}
